﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace RKConsulting.Web.Helpers
{
    public static class QueryHelper
    {
        public static int? GetNullableIntQueryParam(string key)
        {
            var param = HttpContext.Current.Request.QueryString[key];
            int value;

            if (int.TryParse(param, out value))
                return value;

            return null;
        }

        public static int GetIntQueryParam(string key)
        {
            var param = HttpContext.Current.Request.QueryString[key];
            return int.Parse(param);
        }

        public static string ConstructUrl(string url, params string[] queries)
        {
            if (queries == null || !queries.Any())
                return url;

            var strUrl = new StringBuilder(url);
            strUrl.Append("?");
            for (var i = 0; i < queries.Length; i++)
            {
                strUrl.Append(queries[i]);
                strUrl.Append(i%2 == 0 ? "&" : "=");
            }

            return strUrl.ToString();
        }
    }
}
